home *** CD-ROM | disk | FTP | other *** search
/ Mastering Web Site Development / Microsoft Mastering Web Site Development (Microsoft) (1997).iso / Labs / Lab08 / enrollment.cls < prev    next >
Encoding:
Visual Basic class definition  |  1997-04-24  |  2.6 KB  |  85 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4. END
  5. Attribute VB_Name = "Enrollment"
  6. Attribute VB_GlobalNameSpace = False
  7. Attribute VB_Creatable = True
  8. Attribute VB_PredeclaredId = False
  9. Attribute VB_Exposed = True
  10. Const MAXCLASSES = 6
  11. Function OverEnrolled(conn As ADODB.Connection, lngStudentID As Long) As Boolean
  12.     Dim rsEnrollment As Object
  13.     
  14.     'Student cannot add if already at class limit
  15.     strSQL = "select count(studentid) from enrollment where studentid = " & lngStudentID
  16.     Set rsEnrollment = CreateObject("ADODB.recordset")
  17.     rsEnrollment.ActiveConnection = conn
  18.     rsEnrollment.Open strSQL, , adOpenKeyset
  19.     If rsEnrollment.Fields(0).Value > MAXCLASSES Then
  20.         OverEnrolled = True
  21.     Else
  22.         OverEnrolled = False
  23.     End If
  24.     rsEnrollment.Close
  25. End Function
  26. Function ClassCompleted(conn As ADODB.Connection, lngStudentID As Long, strClassID As String) As Boolean
  27.     Dim rsEnrollment As Object
  28.     
  29.     'Student cannot drop a class if the class is completed
  30.     'In other words, it has a grade
  31.     strSQL = "select classid,studentid,grade from enrollment where studentid = " & lngStudentID & " AND classid ='" & strClassID & "'"
  32.     Set rsEnrollment = conn.Execute(strSQL)
  33.     
  34.     'Check for no records even existing
  35.     If rsEnrollment.BOF And rsEnrollment.EOF Then
  36.        ClassCompleted = False
  37.        Exit Function
  38.     End If
  39.     
  40.     'Check for a non-null grade
  41.     If Not IsNull(rsEnrollment.Fields("Grade")) Then
  42.         ClassCompleted = True
  43.     Else
  44.         ClassCompleted = False
  45.     End If
  46.     rsEnrollment.Close
  47. End Function
  48. Public Function Add(ByVal lngStudentID As Long, ByVal strClassID As String) As Integer
  49.  
  50.     Dim conn As ADODB.Connection
  51.     Dim strSQL As String
  52.     
  53.     'Set return value to 0
  54.     Add = 0
  55.     Set conn = CreateObject("ADODB.Connection")
  56.     On Error GoTo ErrorHandler
  57.     'Open connection with State University Database
  58.     conn.Open "DSN=StateU;UID=sa;PWD=;"
  59.      
  60.     'Business rule
  61.     'See if student is over enrolled
  62.     If OverEnrolled(conn, lngStudentID) Then
  63.         Add = 1
  64.         Exit Function
  65.     End If
  66.     
  67.     'Perform the work of adding the student to the class
  68.     strSQL = "INSERT INTO enrollment (ClassID, StudentID) VALUES ('" & strClassID & "'," & lngStudentID & ")"
  69.     conn.BeginTrans
  70.     conn.Execute strSQL
  71.     conn.CommitTrans
  72.     
  73.     'Close the connection
  74.     conn.Close
  75.     Exit Function
  76.  
  77. ErrorHandler:
  78.     'Rollback the transaction if we got that far
  79.     If Not IsEmpty(conn) Then conn.RollbackTrans
  80.     
  81.     'Raise an error to report back
  82.     Add = 1
  83. End Function
  84.  
  85.